package com.lmj.learnspringboot.config;


import com.lmj.learnspringboot.interceptor.ResponseResultInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

/**
 * 响应过滤器配置器
 *
 * @author: lmj
 * @version: GlobalHttpCodeConfig.java lmj v1.0 2021/8/8 02:02
 */

@Configuration
public class GlobalHttpCodeConfig extends WebMvcConfigurerAdapter {

    /**
     * 响应结果过滤器配置
     *
     * @return 过滤器
     */
    @Bean
    public ResponseResultInterceptor responseResultInterceptor() {
        return new ResponseResultInterceptor();
    }

    /**
     * 解决String 统一封装Result的问题
     *
     * @param converters 转换器集合
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(0, new MappingJackson2HttpMessageConverter());
    }

    /**
     * 拦截正则表达式
     *
     * @param registry 拦截器注册器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(responseResultInterceptor()).addPathPatterns("/**");
    }
}
